package com.sunwayworld.dev;

import java.sql.SQLException;

import com.sunwayworld.dev.core.generate.GenerateFile;
//import com.sunwayworld.dev.core.generate.GenerateHibernateXML;
import com.sunwayworld.dev.core.generate.GenerateJAVA;
import com.sunwayworld.dev.core.valueobject.JavaConst;
import com.sunwayworld.dev.core.valueobject.JavaObject;
import com.sunwayworld.dev.util.DevConfigUtil;
import com.sunwayworld.dev.util.StringDevUtil;

public class SimpleTools {

	private JavaObject _valueObject = null;

	private JavaObject _interface = null;

	private JavaObject _action = null;

	private String modueName = null;

	private String packageName = null;

	public SimpleTools(String packageName, String tableName)
			throws SQLException {
		modueName = StringDevUtil.convertTableName(tableName);
		this.packageName=packageName;
		_valueObject = new JavaObject(packageName + ".valueobject." + modueName
				+ "Info", "/template-freemarker/valueobject.jpa.ftl", tableName);
		//现在采用 hibernate3.2+jpa 不再生成传统的 vo
//		_valueObject = new JavaObject(packageName + ".valueobject." + modueName
//				+ "Info", "/template-freemarker/valueobject.ftl", tableName);
	}

	public JavaObject getValueObject() {
		return _valueObject;
	}

	public JavaObject getInterface() throws SQLException {
		if (null == _interface) {
			_interface = new JavaObject(packageName + ".service." + modueName
					+ "Manager", "/template-freemarker/interface.ftl", null);
			_interface.description = "接口";
			_interface.putContext(JavaConst.valueobject, getValueObject());
		}

		return _interface;
	}

	public JavaObject getAction() throws SQLException {
		if (null == _action) {
			_action = new JavaObject(packageName + ".action." + modueName
					+ "Action", "/template-freemarker/action.ftl", null);

			_action.putContext(JavaConst.Interface, getInterface());
			_action.putContext(JavaConst.valueobject, getValueObject());

		}
		return _action;
	}

	public boolean generateValueObject() {

		try {
			GenerateJAVA g = new GenerateJAVA(_valueObject);
			g.generateCode();
			return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}

	public boolean generateService() {
		try {

			GenerateJAVA g_interface = new GenerateJAVA(getInterface());
			g_interface.generateCode();

			JavaObject serviceImpl = new JavaObject(packageName + ".service."
					+ modueName + "ManagerImpl",
					"/template-freemarker/interfaceImpl.spring.ftl", null);
			serviceImpl.description = "实现";
			serviceImpl.putContext(JavaConst.Interface, getInterface());
			serviceImpl.putContext(JavaConst.valueobject, getValueObject());
			GenerateJAVA g_impl = new GenerateJAVA(serviceImpl);
			g_impl.generateCode();

			return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}

	}

	public boolean generateAction() {
		try {
			GenerateJAVA g = new GenerateJAVA(getAction());
			g.generateCode();
			return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}

//	public boolean generateHibernateXML() {
//
//		try {
//			JavaObject valueobject = getValueObject();
//			valueobject.setVmPath("/template-freemarker/hibernate.hbm.xml.ftl");
//			GenerateHibernateXML g = new GenerateHibernateXML(valueobject);
//
//			g.generateCode();
//			return true;
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//			return false;
//		}
//
//	}

	public boolean generateStruts2XML() {
		try {
			JavaObject action = getAction();
			action.setSrcPath("/");
			String srcFile = action.getBaseSrc() + "/" + modueName.toLowerCase()
					+ ".xml";
			GenerateFile g = new GenerateFile(
					"/template-freemarker/struts.xml.ftl", srcFile);
			g.putContext("modueName",modueName);
			g.putContext("action",action);
			g.generateCode();
			return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}
	
	public boolean generateStruts2Ftl() {
		try {
			String baseWeb = DevConfigUtil.getProperty("web");
			baseWeb+="/WEB-INF/freemarker/";
			
			String src=baseWeb+this.modueName.toLowerCase()+"/"+this.modueName.toLowerCase()+"List.ftl";
			
			GenerateFile g = new GenerateFile(
					"/template-freemarker/list.ftl", src);
			g.putContext("modueName",modueName);
			g.putContext("action", getAction());
			g.putContext(JavaConst.valueobject, getValueObject());
			
			g.generateCode();
			
			String src_detail=baseWeb+this.modueName.toLowerCase()+"/"+this.modueName.toLowerCase()+"Detail.ftl";
			
			g = new GenerateFile(
					"/template-freemarker/Detail.ftl", src_detail);
			g.putContext("modueName",modueName);
			g.putContext("action", getAction());
			g.putContext(JavaConst.valueobject, getValueObject());
			g.generateCode();
			return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}

	public void generateAll() {
		this.generateValueObject();
//		this.generateHibernateXML();
		this.generateService();
		this.generateAction();
		this.generateStruts2XML();
		this.generateStruts2Ftl();

	}
}
